package com.webdev.newtech.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.webdev.newtech.domain.SysUser;
import com.webdev.newtech.service.ISysUserService;
import com.webdev.newtech.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("sysUser")
public class SysUserController {

    //等价于@Autowired
    @Resource
    ISysUserService userService;

    @GetMapping("add")
    public AjaxResult addUser(SysUser user){
        try {
            return AjaxResult.success(userService.add(user));
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("{userId}/remove")
    public AjaxResult remove(@PathVariable("userId") Integer userId){
        try {
            return AjaxResult.success(userService.removeUserById(userId));
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }


    @GetMapping("update")
    public AjaxResult update(SysUser user){
        System.out.println(user);
        System.out.println("更新");
        return AjaxResult.success(userService.update(user));

    }

    @PostMapping("getUser")
    public AjaxResult getUserById(Integer userId){
        if(userId==null){
            return AjaxResult.error("请传递要查询的用户ID");
        }
        return AjaxResult.success( userService.getUserById(userId) );
    }

    @PostMapping("getUserList")
    public  AjaxResult getUserList(SysUser user){
        return AjaxResult.success(userService.getUserList(user));
    }


    @PostMapping("getList")
    public  AjaxResult getList(Integer pageNum,Integer pageSize,SysUser user){
        PageHelper.startPage(pageNum,pageSize);
        List<SysUser> list = userService.getUserList(user);
        PageInfo info = new PageInfo<SysUser>(list);
        return AjaxResult.success(info);
    }

    @GetMapping("info")
    public  AjaxResult getList(){
        return AjaxResult.success("获取用户信息");
    }




}
